package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.ScenicRepComment;
import cn.wolfcode.luowowo.comment.query.ScenicRepCommentQuery;
import cn.wolfcode.luowowo.comment.repository.IScenicRepCommentRepository;
import cn.wolfcode.luowowo.comment.service.IScenicRepCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class ScenicRepCommentServiceImpl implements IScenicRepCommentService {

    @Autowired
    private IScenicRepCommentRepository repository;

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public ScenicRepComment save(ScenicRepComment comment) {

        if(!StringUtils.hasLength(comment.getId())){
            comment.setCreateTime(new Date());
        }

        repository.save(comment);

        return comment;
    }

    @Override
    public Page query(ScenicRepCommentQuery qo) {
        //repository  mongoTemplate 无法直接查询出page分页数据对象, 所以需要自己手动封装
        //拼接条件
        Query query = new Query();
        /*if(qo.getScenicId() != -1){
            query.addCriteria(Criteria.where("commentId").is(qo.getDetailId()));
        }*/
        //查总数: select count(id) from xxx where ....
        long total = mongoTemplate.count(query, ScenicRepComment.class);
        if(total == 0){
            return Page.empty();  //空的分页对象
        }
        //查当前页显示list : select * from xxx where ... limit...
        //注意:需要设置分页信息, 排序信息
        //参数1:当前页, 从0开始, 参数2:每页显示条数  参数3:排序规则, 参数4:排序字段
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(),
                Sort.Direction.DESC, "createTime") ;
        query.with(pageable);

        List<ScenicRepComment> list = mongoTemplate.find(query, ScenicRepComment.class);
        return new PageImpl(list, pageable, total);
    }

    @Override
    public Page queryByCommentId(String commentId) {
        //repository  mongoTemplate 无法直接查询出page分页数据对象, 所以需要自己手动封装
        //拼接条件
        Query query = new Query();
        if(commentId !=null){
            query.addCriteria(Criteria.where("commentId").is(commentId));
        }
        //查总数: select count(id) from xxx where ....
        long total = mongoTemplate.count(query, ScenicRepComment.class);
        if(total == 0){
            return Page.empty();  //空的分页对象
        }
        //查当前页显示list : select * from xxx where ... limit...
        //注意:需要设置分页信息, 排序信息
        //参数1:当前页, 从0开始, 参数2:每页显示条数  参数3:排序规则, 参数4:排序字段
        Pageable pageable = PageRequest.of(0,5,
                Sort.Direction.DESC, "createTime") ;
        query.with(pageable);

        List<ScenicRepComment> list = mongoTemplate.find(query, ScenicRepComment.class);
        return new PageImpl(list, pageable, total);
    }
}
